/**
 * Copyright (C) 2009 SC 4ViewSoft SRL
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ru.spbu.math.android.cbrf.charts;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
import ru.spbu.math.android.cbrf.charts.library.chart.AbstractChart;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * The view that encapsulates the graphical chart.
 */
public class GraphicalView extends View implements OnTouchListener {
    /**
	 * The chart to be drawn.
	 * @uml.property  name="mChart"
	 * @uml.associationEnd  
	 */
    private AbstractChart mChart;
    /**
     * The view bounds.
     */
    private Rect mRect = new Rect();
    /**
     * The user interface thread handler.
     */
    private Handler mHandler;

    private ChartsActivity chartsActivity;
    /**
     * Creates a new graphical view.
     *
     * @param context the context
     * @param chart   the chart to be drawn
     */
    public GraphicalView(Context context, AbstractChart chart) {
        super(context);
        mChart = chart;
        mHandler = new Handler();
        this.setOnTouchListener(this);       
    }
    
    public GraphicalView(Context context, AbstractChart chart, ChartsActivity ca) {
        super(context);
        mChart = chart;
        mHandler = new Handler();
        this.setOnTouchListener(this);       
        chartsActivity = ca;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.getClipBounds(mRect);
        int top = mRect.top;
        int left = mRect.left;
        int width = mRect.width();
        int height = mRect.height();
        mChart.draw(canvas, left, top, width, height);
    }

    /**
     * Schedule a user interface repaint.
     */
    public void repaint() {
        mHandler.post(new Runnable() {
            public void run() {
                invalidate();
            }
        });
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ChartsActivity ca = chartsActivity;
        final int range = 3;
        Context context = v.getContext();

        DateValue dv = chartsActivity.getDateValueByXY(event.getX(), event.getY());
        if (dv != null) {
            CharSequence text = dv.toString();
            Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.TOP | Gravity.LEFT, (int) event.getX(), (int) event.getY());
            toast.show();
        } else {
            if (event.getX() < 15) {
                Calendar start = ca.getStartDate();
                start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), start.get(Calendar.DAY_OF_MONTH) - range);
                Calendar end = ca.getEndDate();
                end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH), end.get(Calendar.DAY_OF_MONTH) - range);
                ca.setDateRange(start, end);
            }
            if (event.getX() > this.getWidth() - 15) {
                Calendar start = ca.getStartDate();
                start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), start.get(Calendar.DAY_OF_MONTH) + range);
                Calendar end = ca.getEndDate();
                end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH), end.get(Calendar.DAY_OF_MONTH) + range);
                if (end.before(new GregorianCalendar())) ca.setDateRange(start, end);
            }
        }
        return false;
    }
}